home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / unzip42.zip / EXTRACT.C < prev    next >
C/C++ Source or Header  |  1992-03-20  |  31KB  |  818 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   extract.c
  4.  
  5.   This file contains the high-level routines ("driver routines") for extrac-
  6.   ting and testing zipfile members.  It calls the low-level routines in files
  7.   inflate.c, unimplod.c, unreduce.c and unshrink.c.
  8.  
  9.   ---------------------------------------------------------------------------*/
  10.  
  11.  
  12. #include "unzip.h"
  13.  
  14.  
  15. /************************************/
  16. /*  Extract Local Prototypes, etc.  */
  17. /************************************/
  18.  
  19. static int store_info __((void));
  20. static int extract_or_test_member __((void));
  21. #ifdef CRYPT
  22.    static int decrypt_member __((void));
  23. #endif
  24.  
  25. static char *VersionMsg =
  26.   " skipping: %-22s  need %s compat. v%u.%u (can do v%u.%u)\n";
  27. static char *ComprMsg =
  28.   " skipping: %-22s  compression method %d\n";
  29. static char *FilNamMsg =
  30.   "%s:  bad filename length (%s)\n";
  31. static char *ExtFieldMsg =
  32.   "%s:  bad extra field length (%s)\n";
  33. static char *OffsetMsg =
  34.   "file #%d:  bad zipfile offset (%s)\n";
  35.  
  36.  
  37.  
  38.  
  39.  
  40. /**************************************/
  41. /*  Function extract_or_test_files()  */
  42. /**************************************/
  43.  
  44. int extract_or_test_files()    /* return PK-type error code */
  45. {
  46.     char **fnamev;
  47.     byte *cd_inptr;
  48.     int cd_incnt, error, error_in_archive=0;
  49.     int renamed, query, len, filnum=(-1), blknum=0;
  50.     UWORD i, j, members_remaining, num_skipped=0, num_bad_pwd=0;
  51.     longint cd_bufstart, bufstart, inbuf_offset, request;
  52.     min_info info[DIR_BLKSIZ];
  53.  
  54.  
  55. /*---------------------------------------------------------------------------
  56.     The basic idea of this function is as follows.  Since the central di-
  57.     rectory lies at the end of the zipfile and the member files lie at the
  58.     beginning or middle or wherever, it is not very desirable to simply
  59.     read a central directory entry, jump to the member and extract it, and
  60.     then jump back to the central directory.  In the case of a large zipfile
  61.     this would lead to a whole lot of disk-grinding, especially if each mem-
  62.     ber file is small.  Instead, we read from the central directory the per-
  63.     tinent information for a block of files, then go extract/test the whole
  64.     block.  Thus this routine contains two small(er) loops within a very
  65.     large outer loop:  the first of the small ones reads a block of files
  66.     from the central directory; the second extracts or tests each file; and
  67.     the outer one loops over blocks.  There's some file-pointer positioning
  68.     stuff in between, but that's about it.  Btw, it's because of this jump-
  69.     ing around that we can afford to be lenient if an error occurs in one of
  70.     the member files:  we should still be able to go find the other members,
  71.     since we know the offset of each from the beginning of the zipfile.
  72.  
  73.     Begin main loop over blocks of member files.  We know the entire central
  74.     directory is on this disk:  we would not have any of this information un-
  75.     less the end-of-central-directory record was on this disk, and we would
  76.     not have gotten to this routine unless this is also the disk on which
  77.     the central directory starts.  In practice, this had better be the ONLY
  78.     disk in the archive, but maybe someday we'll add multi-disk support.
  79.   ---------------------------------------------------------------------------*/
  80.  
  81.     pInfo = info;
  82.     members_remaining = ecrec.total_entries_central_dir;
  83.  
  84.     while (members_remaining) {
  85.         j = 0;
  86.  
  87.         /*
  88.          * Loop through files in central directory, storing offsets, file
  89.          * attributes, and case-conversion flags until block size is reached.
  90.          */
  91.  
  92.         while (members_remaining && (j < DIR_BLKSIZ)) {
  93.             --members_remaining;
  94.             pInfo = &info[j];
  95.  
  96.             if (readbuf(sig, 4) <= 0) {
  97.                 error_in_archive = 51;  /* 51:  unexpected EOF */
  98.                 members_remaining = 0;  /* ...so no more left to do */
  99.                 break;
  100.             }
  101.             if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  102.                 fprintf(stderr, CentSigMsg, j);  /* sig not found */
  103.                 fprintf(stderr, ReportMsg);   /* check binary transfers */
  104.                 error_in_archive = 3;   /* 3:  error in zipfile */
  105.                 members_remaining = 0;  /* ...so no more left to do */
  106.                 break;
  107.             }
  108.             /* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */
  109.             if ((error = process_cdir_file_hdr()) != 0) {
  110.                 error_in_archive = error;   /* only 51 (EOF) defined */
  111.                 members_remaining = 0;  /* ...so no more left to do */
  112.                 break;
  113.             }
  114.             if ((error = do_string(crec.filename_length, FILENAME)) != 0) {
  115.                 if (error > error_in_archive)
  116.                     error_in_archive = error;
  117.                 if (error > 1) {  /* fatal:  no more left to do */
  118.                     fprintf(stderr, FilNamMsg, filename, "central");
  119.                     members_remaining = 0;
  120.                     break;
  121.                 }
  122.             }
  123.             if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0)
  124.             {
  125.                 if (error > error_in_archive)
  126.                     error_in_archive = error;
  127.                 if (error > 1) {  /* fatal */
  128.                     fprintf(stderr, ExtFieldMsg, filename, "central");
  129.                     members_remaining = 0;
  130.                     break;
  131.                 }
  132.             }
  133.             if ((error = do_string(crec.file_comment_length, SKIP)) != 0) {
  134.                 if (error > error_in_archive)
  135.                     error_in_archive = error;
  136.                 if (error > 1) {  /* fatal */
  137.                     fprintf(stderr, "\n%s:  bad file comment length\n",
  138.                             filename);
  139.                     members_remaining = 0;
  140.                     break;
  141.                 }
  142.             }
  143.             if (process_all_files) {
  144.                 if (store_info())
  145.                     ++num_skipped;
  146.                 else
  147.                     ++j;  /* file is OK: save info[] and continue with next */
  148.             } else {
  149.                 fnamev = fnv;   /* don't destroy permanent filename pointer */
  150.                 for (--fnamev; *++fnamev;)
  151.                     if (match(filename, *fnamev)) {
  152.                         if (store_info())
  153.                             ++num_skipped;
  154.                         else
  155.                             ++j;   /* file is OK */
  156.                         break;  /* found match for filename, so stop looping */
  157.                     } /* end if (match), for-loop (fnamev) */
  158.             } /* end if (process_all_files) */
  159.  
  160.         } /* end while-loop (adding files to current block) */
  161.  
  162.         /* save position in central directory so can come back later */
  163.         cd_bufstart = cur_zipfile_bufstart;
  164.         cd_inptr = inptr;
  165.         cd_incnt = incnt;
  166.  
  167.     /*-----------------------------------------------------------------------
  168.         Second loop:  process files in current block, extracting or testing
  169.         each one.
  170.       -----------------------------------------------------------------------*/
  171.  
  172.         for (i = 0; i < j; ++i) {
  173.             filnum = i + blknum*DIR_BLKSIZ;
  174.             pInfo = &info[i];
  175.             /*
  176.              * if the target position is not within the current input buffer
  177.              * (either haven't yet read far enough, or (maybe) skipping back-
  178.              * ward) skip to the target position and reset readbuf().
  179.              */
  180.             /* LSEEK(pInfo->offset):  */
  181.             request = pInfo->offset + extra_bytes;
  182.             inbuf_offset = request % INBUFSIZ;
  183.             bufstart = request - inbuf_offset;
  184.  
  185.             if (request < 0) {
  186.                 fprintf(stderr, SeekMsg, ReportMsg);
  187.                 error_in_archive = 3;       /* 3:  severe error in zipfile, */
  188.                 continue;                   /*  but can still go on */
  189.             } else if (bufstart != cur_zipfile_bufstart) {
  190.                 cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET);
  191.                 if ((incnt = read(zipfd,(char *)inbuf,INBUFSIZ)) <= 0) {
  192.                     fprintf(stderr, OffsetMsg, filnum, "lseek");
  193.                     error_in_archive = 3;   /* 3:  error in zipfile, but */
  194.                     continue;               /*  can still do next file   */
  195.                 }
  196.                 inptr = inbuf + inbuf_offset;
  197.                 incnt -= inbuf_offset;
  198.             } else {
  199.                 incnt += (inptr-inbuf) - inbuf_offset;
  200.                 inptr = inbuf + inbuf_offset;
  201.             }
  202.  
  203.             /* should be in proper position now, so check for sig */
  204.             if (readbuf(sig, 4) <= 0) {  /* bad offset */
  205.                 fprintf(stderr, OffsetMsg, filnum, "EOF");
  206.                 error_in_archive = 3;    /* 3:  error in zipfile */
  207.                 continue;       /* but can still try next one */
  208.             }
  209.             if (strncmp(sig, local_hdr_sig, 4)) {
  210.                 fprintf(stderr, OffsetMsg, filnum,
  211.                         "can't find local header sig");   /* bad offset */
  212.                 error_in_archive = 3;
  213.                 continue;
  214.             }
  215.             if ((error = process_local_file_hdr()) != 0) {
  216.                 fprintf(stderr, "\nfile #%d:  bad local header\n", filnum);
  217.                 error_in_archive = error;       /* only 51 (EOF) defined */
  218.                 continue;       /* can still try next one */
  219.             }
  220.             if ((error = do_string(lrec.filename_length, FILENAME)) != 0) {
  221.                 if (error > error_in_archive)
  222.                     error_in_archive = error;
  223.                 if (error > 1) {
  224.                     fprintf(stderr, FilNamMsg, filename, "local");
  225.                     continue;   /* go on to next one */
  226.                 }
  227.             }
  228.             if ((error = do_string(lrec.extra_field_length, EXTRA_FIELD)) != 0)
  229.             {
  230.                 if (error > error_in_archive)
  231.                     error_in_archive = error;
  232.                 if (error > 1) {
  233.                     fprintf(stderr, ExtFieldMsg, filename, "local");
  234.                     continue;   /* go on */
  235.                 }
  236.             }
  237.  
  238.             /*
  239.              * just about to extract file:  if extracting to disk, check if
  240.              * already exists, and if so, take appropriate action according to
  241.              * fflag/uflag/overwrite_all/etc. (we couldn't do this in upper
  242.              * loop because we don't store the possibly renamed filename[] in
  243.              * info[])
  244.              */
  245.             if (!tflag && !cflag) {
  246.                 renamed = FALSE;   /* user hasn't renamed output file yet */
  247. startover:
  248.                 query = FALSE;
  249.                 /* mapname can create dirs if not freshening or if renamed */
  250.                 if ((error = mapname(!fflag || renamed)) > 1) {    /* skip */
  251.                     if ((error > 2) && (error_in_archive < 2))
  252.                         error_in_archive = 2;   /* (weak) error in zipfile */
  253.                     continue;   /* go on to next file */
  254.                 }
  255.  
  256.                 switch (check_for_newer(filename)) {
  257.                     case DOES_NOT_EXIST:
  258.                         if (fflag && !renamed)  /* don't skip if just renamed */
  259.                             continue;   /* freshen (no new files):  skip */
  260.                         break;
  261.                     case EXISTS_AND_OLDER:
  262.                         if (overwrite_none)
  263.                             continue;   /* never overwrite:  skip file */
  264.                         if (!overwrite_all && !force_flag)
  265.                             query = TRUE;
  266.                         break;
  267.                     case EXISTS_AND_NEWER:             /* (or equal) */
  268.                         if (overwrite_none || (uflag && !renamed))
  269.                             continue;  /* skip if update/freshen & orig name */
  270.                         if (!overwrite_all && !force_flag)
  271.                             query = TRUE;
  272.                         break;
  273.                 }
  274. #ifndef VMS     /* VMS creates higher version number instead of overwriting
  275.                  * (will have to modify for VMS-style names with specific
  276.                  *  version numbers:  just check V_flag?  don't use stat?) */
  277.                 if (query) {
  278.                     fprintf(stderr,
  279.                       "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
  280.                       filename);
  281.                     FFLUSH   /* for Amiga and Mac MPW */
  282.                     fgets(answerbuf, 9, stdin);
  283.                     switch (*answerbuf) {
  284.                         case 'A':   /* dangerous option:  force caps */
  285.                             overwrite_all = TRUE;
  286.                             overwrite_none = FALSE;  /* just to make sure */
  287.                             break;
  288.                         case 'r':
  289.                         case 'R':
  290.                             do {
  291.                                 fprintf(stderr, "new name: ");
  292.                                 FFLUSH   /* for AMIGA and Mac MPW */
  293.                                 fgets(filename, FILNAMSIZ, stdin);
  294.                                 /* usually get \n here: better check for it */
  295.                                 len = strlen(filename);
  296.                                 if (filename[len-1] == '\n')
  297.                                     filename[--len] = 0;
  298.                             } while (len == 0);
  299.                             renamed = TRUE;
  300.                             goto startover;   /* sorry for a goto */
  301.                         /*  break;   (never reached--dpk) */
  302.                         case 'y':
  303.                         case 'Y':
  304.                             break;
  305.                         case 'N':
  306.                             overwrite_none = TRUE;
  307.                             overwrite_all = FALSE;  /* make sure */
  308.                             force_flag = FALSE;     /* ditto */
  309.                             /* FALL THROUGH, skip */
  310.                         case 'n':
  311.                         default:
  312.                             continue;   /* skip file */
  313.                     } /* end switch (*answerbuf) */
  314.                 } /* end if (query) */
  315. #endif /* !VMS */
  316.             } /* end if (extracting to disk) */
  317.  
  318. #ifdef CRYPT
  319.             if (pInfo->encrypted && ((error = decrypt_member()) != 0)) {
  320.                 fprintf(stderr, " skipping: %-22s  incorrect password\n",
  321.                   filename);
  322.                 ++num_bad_pwd;
  323.             /*  if (error > error_in_archive)   >>until fix up error return<<
  324.                     error_in_archive = error;    unable to test file... */
  325.                 continue;   /* go on to next file */
  326.                 /* GRR:  add loop here (2 or 3 times) for bad passwords */
  327.             }
  328. #endif /* CRYPT */
  329.             disk_full = 0;
  330.             if ((error = extract_or_test_member()) != 0) {
  331.                 if (error > error_in_archive)
  332.                     error_in_archive = error;       /* ...and keep going */
  333.                 if (disk_full > 1)
  334.                     return error_in_archive;        /* (unless disk full) */
  335.             }
  336.         } /* end for-loop (i:  files in current block) */
  337.  
  338.  
  339.         /*
  340.          * Jump back to where we were in the central directory, then go and do
  341.          * the next batch of files.
  342.          */
  343.  
  344.         cur_zipfile_bufstart = lseek(zipfd, cd_bufstart, SEEK_SET);
  345.         read(zipfd, (char *)inbuf, INBUFSIZ);  /* were there b4 ==> no error */
  346.         inptr = cd_inptr;
  347.         incnt = cd_incnt;
  348.         ++blknum;
  349.  
  350. #ifdef TEST
  351.         printf("\ncd_bufstart = %ld (%.8lXh)\n", cd_bufstart, cd_bufstart);
  352.         printf("cur_zipfile_bufstart = %ld (%.8lXh)\n", cur_zipfile_bufstart,
  353.           cur_zipfile_bufstart);
  354.         printf("inptr-inbuf = %d\n", inptr-inbuf);
  355.         printf("incnt = %d\n\n", incnt);
  356. #endif
  357.  
  358.     } /* end while-loop (blocks of files in central directory) */
  359.  
  360. /*---------------------------------------------------------------------------
  361.     Double-check that we're back at the end-of-central-directory record, and
  362.     print quick summary of results, if we were just testing the archive.  We
  363.     send the summary to stdout so that people doing the testing in the back-
  364.     ground and redirecting to a file can just do a "tail" on the output file.
  365.   ---------------------------------------------------------------------------*/
  366.  
  367.     readbuf(sig, 4);
  368.     if (strncmp(sig, end_central_sig, 4)) {     /* just to make sure again */
  369.         fprintf(stderr, EndSigMsg);  /* didn't find end-of-central-dir sig */
  370.         fprintf(stderr, ReportMsg);  /* check binary transfers */
  371.         if (!error_in_archive)       /* don't overwrite stronger error */
  372.             error_in_archive = 1;    /* 1:  warning error */
  373.     }
  374.     if (tflag && (quietflg == 1)) {
  375.         int num=filnum+1 - num_bad_pwd;
  376.  
  377.         if (error_in_archive)
  378.             printf("At least one error was detected in %s.\n", zipfn);
  379.         else if (num == 0)
  380.             printf("Caution:  zero files tested in %s.\n", zipfn);
  381.         else if (process_all_files && (num_skipped+num_bad_pwd == 0))
  382.             printf("No errors detected in %s.\n", zipfn);
  383.         else
  384.             printf("No errors detected in %s for the %d file%s tested.\n",
  385.               zipfn, num, (num==1)? "":"s");
  386.         if (num_skipped > 0)
  387.             printf("%d file%s skipped because of unsupported compression or\
  388.  encoding.\n",
  389.               num_skipped, (num_skipped==1)? "":"s");
  390. #ifdef CRYPT
  391.         if (num_bad_pwd > 0)
  392.             printf("%d file%s skipped because of incorrect password.\n",
  393.               num_bad_pwd, (num_bad_pwd==1)? "":"s");
  394. #endif /* CRYPT */
  395.     }
  396.     if ((num_skipped > 0) && !error_in_archive)   /* files not tested or  */
  397.         error_in_archive = 1;                     /*  extracted:  warning */
  398. #ifdef CRYPT
  399.     if ((num_bad_pwd > 0) && !error_in_archive)   /* files not tested or  */
  400.         error_in_archive = 1;                     /*  extracted:  warning */
  401. #endif /* CRYPT */
  402.  
  403.     return (error_in_archive);
  404.  
  405. } /* end function extract_or_test_files() */
  406.  
  407.  
  408.  
  409.  
  410.  
  411. /***************************/
  412. /*  Function store_info()  */
  413. /***************************/
  414.  
  415. static int store_info()   /* return 1 if skipping, 0 if OK */
  416. {
  417.     ULONG tmp;
  418.  
  419. #ifdef INFLATE
  420. #  define UNKN_COMPR \
  421.    (crec.compression_method>IMPLODED && crec.compression_method!=DEFLATED)
  422. #else
  423. #  define UNKN_COMPR   (crec.compression_method>IMPLODED)
  424. #endif
  425.  
  426.  
  427. /*---------------------------------------------------------------------------
  428.     Check central directory info for version/compatibility requirements.
  429.   ---------------------------------------------------------------------------*/
  430.  
  431.     pInfo->encrypted = crec.general_purpose_bit_flag & 1;    /* bit field */
  432.     pInfo->ExtLocHdr = (crec.general_purpose_bit_flag & 8) == 8;  /* bit */
  433.     pInfo->text = crec.internal_file_attributes & 1;         /* bit field */
  434. /*  pInfo->crc = crec.crc32;  (no longer used) */
  435.  
  436.     if (crec.version_needed_to_extract[1] == VMS_) {
  437.         if (crec.version_needed_to_extract[0] > VMS_VERSION) {
  438.             fprintf(stderr, VersionMsg, filename, "VMS",
  439.               crec.version_needed_to_extract[0] / 10,
  440.               crec.version_needed_to_extract[0] % 10,
  441.               VMS_VERSION / 10, VMS_VERSION % 10);
  442.             return 1;
  443.         }
  444. #ifndef VMS   /* won't be able to use extra field, but still have data */
  445.         else if (!tflag && !force_flag) {  /* if forcing, extract regardless */
  446.             fprintf(stderr,
  447.               "\n%s:  stored in VMS format.  Extract anyway? (y/n) ",
  448.               filename);
  449.             FFLUSH   /* for Amiga and Mac MPW */
  450.             fgets(answerbuf, 9, stdin);
  451.             if ((*answerbuf != 'y') && (*answerbuf != 'Y'))
  452.                 return 1;
  453.         }
  454. #endif /* !VMS */
  455.     /* usual file type:  don't need VMS to extract */
  456.     } else if (crec.version_needed_to_extract[0] > UNZIP_VERSION) {
  457.         fprintf(stderr, VersionMsg, filename, "PK",
  458.           crec.version_needed_to_extract[0] / 10,
  459.           crec.version_needed_to_extract[0] % 10,
  460.           UNZIP_VERSION / 10, UNZIP_VERSION % 10);
  461.         return 1;
  462.     }
  463.  
  464.     if UNKN_COMPR {
  465.         fprintf(stderr, ComprMsg, filename, crec.compression_method);
  466.         return 1;
  467.     }
  468. #ifndef CRYPT
  469.     if (pInfo->encrypted) {
  470.         fprintf(stderr, " skipping: %-22s  encrypted (not supported)\n",
  471.           filename);
  472.         return 1;
  473.     }
  474. #endif /* !CRYPT */
  475.  
  476. /*---------------------------------------------------------------------------
  477.     Store some central-directory information (encryption, file attributes,
  478.     offsets) for later use.
  479.   ---------------------------------------------------------------------------*/
  480.  
  481.     tmp = crec.external_file_attributes;
  482.  
  483.     pInfo->dos_attr = 32;   /* set archive bit:  file is not backed up */
  484.     switch (pInfo->hostnum) {
  485.         case UNIX_:
  486.         case VMS_:
  487.             pInfo->unix_attr = tmp >> 16;
  488.             break;
  489.         case DOS_OS2_FAT_:
  490.         case OS2_HPFS_:
  491.             pInfo->dos_attr = (unsigned) tmp;
  492.             tmp = (!(tmp & 1)) << 1;   /* read-only bit */
  493.             pInfo->unix_attr = 0444 | (tmp<<6) | (tmp<<3) | tmp;
  494. #ifdef UNIX
  495.             umask( (int)(tmp=umask(0)) );
  496.             pInfo->unix_attr &= ~tmp;
  497. #endif
  498.             break;
  499.         case MAC_:
  500.             tmp &= 1;   /* read-only bit */
  501.             pInfo->unix_attr = tmp;
  502.             break;
  503.         default:
  504.             pInfo->unix_attr = 0666;
  505.             break;
  506.     } /* end switch (host-OS-created-by) */
  507.  
  508.     pInfo->offset = (longint) crec.relative_offset_local_header;
  509.     return 0;
  510.  
  511. } /* end function store_info() */
  512.  
  513.  
  514.  
  515.  
  516.  
  517. /***************************************/
  518. /*  Function extract_or_test_member()  */
  519. /***************************************/
  520.  
  521. static int extract_or_test_member()    /* return PK-type error code */
  522. {
  523. #ifdef S_IFLNK
  524.     int symlnk=FALSE;
  525. #endif /* S_IFLNK */
  526.     int error=0;
  527.     UWORD b;
  528.  
  529.  
  530.  
  531. /*---------------------------------------------------------------------------
  532.     Initialize variables, buffers, etc.
  533.   ---------------------------------------------------------------------------*/
  534.  
  535.     bits_left = 0;
  536.     bitbuf = 0L;
  537.     outpos = 0L;
  538.     outcnt = 0;
  539.     outptr = outbuf;
  540.     zipeof = 0;
  541.     crc32val = 0xFFFFFFFFL;
  542.  
  543. #ifdef S_IFLNK
  544.     if ((pInfo->unix_attr & S_IFMT) == S_IFLNK  &&  (pInfo->hostnum == UNIX_)
  545.         && !tflag && !cflag)
  546.         symlnk = TRUE;
  547. #endif /* S_IFLNK */
  548.  
  549.     memset(outbuf, 0, OUTBUFSIZ);
  550. #ifndef DOS_OS2
  551.     if (aflag)                  /* if we have a scratchpad, clear it out */
  552.         memset(outout, 0, OUTBUFSIZ);
  553. #endif /* !DOS_OS2 */
  554.  
  555.     if (tflag) {
  556.         if (!quietflg) {
  557.             fprintf(stdout, "  Testing: %-22s ", filename);
  558.             fflush(stdout);
  559.         }
  560.     } else {
  561.         if (cflag) {            /* output to stdout (copy of it) */
  562. #if defined(MACOS) || defined(AMIGA)
  563.             outfd = 1;
  564. #else /* !(MACOS || AMIGA) */
  565.             outfd = dup(1);     /* GRR: change this to #define for Mac/Amiga */
  566. #endif /* ?(MACOS || AMIGA) */
  567. #ifdef DOS_OS2
  568.             if (!aflag)
  569.                 setmode(outfd, O_BINARY);
  570. #endif /* DOS_OS2 */
  571. #ifdef VMS
  572.             if (create_output_file())   /* VMS version required for stdout! */
  573.                 return 50;      /* 50:  disk full (?) */
  574. #endif
  575.         } else
  576. #ifdef S_IFLNK
  577.         if (!symlnk)    /* symlink() takes care of file creation */
  578. #endif /* !S_IFLNK */
  579.         {
  580.             if (create_output_file())
  581.                 return 50;      /* 50:  disk full (?) */
  582.         }
  583.     } /* endif (!tflag) */
  584.  
  585. /*---------------------------------------------------------------------------
  586.     Unpack the file.
  587.   ---------------------------------------------------------------------------*/
  588.  
  589.     switch (lrec.compression_method) {
  590.  
  591.     case STORED:
  592.         if (!tflag && QCOND) {
  593.             fprintf(stdout, " Extracting: %-22s ", filename);
  594.             if (cflag)
  595.                 fprintf(stdout, "\n");
  596.             fflush(stdout);
  597.         }
  598. #ifdef S_IFLNK
  599.         /*
  600.          * If file came from Unix and is a symbolic link and we are extracting
  601.          * to disk, allocate a storage area, put the data in it, and create the
  602.          * link.  Since we know it's a symbolic link to start with, shouldn't
  603.          * have to worry about overflowing unsigned ints with unsigned longs.
  604.          * (This doesn't do anything for compressed symlinks, but that can be
  605.          * added later...it also doesn't set the time or permissions of the
  606.          * link, but does anyone really care?)
  607.          */
  608.         if (symlnk) {
  609. #if defined(MTS) || defined(MACOS)
  610.             fprintf(stdout, "\n  warning:  symbolic link ignored\n");
  611.             error = 1;          /* 1:  warning error */
  612. #else /* !(MTS || MACOS) */
  613.             char *orig = (char *)malloc((unsigned)lrec.uncompressed_size+1);
  614.             char *p = orig;
  615.  
  616.             while (ReadByte(&b))
  617.                 *p++ = b;
  618.             *p = 0;   /* terminate string */
  619.             UpdateCRC((unsigned char *)orig, p-orig);
  620.             if (symlink(orig, filename))
  621.                 if ((errno == EEXIST) && overwrite_all) {  /* OK to overwrite */
  622.                     unlink(filename);
  623.                     if (symlink(orig, filename))
  624.                         perror("symlink error");
  625.                 } else
  626.                     perror("symlink error");
  627.             free(orig);
  628. #endif /* ?(MTS || MACOS) */
  629.         } else
  630. #endif /* S_IFLNK */
  631.         while (ReadByte(&b) && !disk_full)
  632.             OUTB(b)
  633.         break;
  634.  
  635.     case SHRUNK:
  636.         if (!tflag && QCOND) {
  637.             fprintf(stdout, "UnShrinking: %-22s ", filename);
  638.             if (cflag)
  639.                 fprintf(stdout, "\n");
  640.             fflush(stdout);
  641.         }
  642. #ifdef S_IFLNK   /* !!! This code needs to be added to unShrink, etc. !!! */
  643.         if (symlnk) {
  644.             fprintf(stdout, "\n  warning:  symbolic link ignored\n");
  645.             error = 1;          /* 1:  warning error */
  646.         }
  647. #endif /* S_IFLNK */
  648.         unShrink();
  649.         break;
  650.  
  651.     case REDUCED1:
  652.     case REDUCED2:
  653.     case REDUCED3:
  654.     case REDUCED4:
  655.         if (!tflag && QCOND) {
  656.             fprintf(stdout, "  Expanding: %-22s ", filename);
  657.             if (cflag)
  658.                 fprintf(stdout, "\n");
  659.             fflush(stdout);
  660.         }
  661. #ifdef S_IFLNK   /* !!! This code needs to be added to unShrink, etc. !!! */
  662.         if (symlnk) {
  663.             fprintf(stdout, "\n  warning:  symbolic link ignored\n");
  664.             error = 1;          /* 1:  warning error */
  665.         }
  666. #endif /* S_IFLNK */
  667.         unReduce();
  668.         break;
  669.  
  670.     case IMPLODED:
  671.         if (!tflag && QCOND) {
  672.             fprintf(stdout, "  Exploding: %-22s ", filename);
  673.             if (cflag)
  674.                 fprintf(stdout, "\n");
  675.             fflush(stdout);
  676.         }
  677. #ifdef S_IFLNK   /* !!! This code needs to be added to unShrink, etc. !!! */
  678.         if (symlnk) {
  679.             fprintf(stdout, "\n  warning:  symbolic link ignored\n");
  680.             error = 1;          /* 1:  warning error */
  681.         }
  682. #endif /* S_IFLNK */
  683.         unImplode();
  684.         break;
  685.  
  686. #ifdef INFLATE
  687.     case DEFLATED:
  688.         if (!tflag && QCOND) {
  689.             fprintf(stdout, "  Deflating: %-22s ", filename);
  690.             if (cflag)
  691.                 fprintf(stdout, "\n");
  692.             fflush(stdout);
  693.         }
  694. #ifdef S_IFLNK   /* !!! This code needs to be added to unShrink, etc. !!! */
  695.         if (symlnk) {
  696.             fprintf(stdout, "\n  warning:  symbolic link ignored\n");
  697.             error = 1;          /* 1:  warning error */
  698.         }
  699. #endif /* S_IFLNK */
  700.         inflate();
  701.         break;
  702. #endif /* INFLATE */
  703.  
  704.     default:   /* should never get to this point */
  705.         fprintf(stderr, "%s:  unknown compression method\n", filename);
  706.         /* close and delete file before return? */
  707.         return 1;               /* 1:  warning error */
  708.  
  709.     } /* end switch (compression method) */
  710.  
  711.     if (disk_full) {            /* set by FlushOutput()/OUTB() macro */
  712.         if (disk_full > 1)
  713.             return 50;          /* 50:  disk full */
  714.         error = 1;              /* 1:  warning error */
  715.     }
  716.  
  717. /*---------------------------------------------------------------------------
  718.     Write the last partial buffer, if any; set the file date and time; and
  719.     close the file (not necessarily in that order).  Then make sure CRC came
  720.     out OK and print result.  [Note:  crc32val must be logical-ANDed with
  721.     32 bits of 1's, or else machines whose longs are bigger than 32 bits will
  722.     report bad CRCs (because of the upper bits being filled with 1's instead
  723.     of 0's).]
  724.   ---------------------------------------------------------------------------*/
  725.  
  726. #ifdef S_IFLNK
  727.     if (!symlnk) {
  728. #endif /* S_IFLNK */
  729.     if (!disk_full && FlushOutput())
  730.         if (disk_full > 1)
  731.             return 50;          /* 50:  disk full */
  732.         else {                  /* disk_full == 1 */
  733.             fprintf(stderr, "%s:  probably corrupt\n", filename);
  734.             error = 1;          /* 1:  warning error */
  735.         }
  736.  
  737.     if (!tflag)
  738. #ifdef VMS
  739.         CloseOutputFile();
  740. #else /* !VMS */
  741. #ifdef MTS                      /* MTS can't set file time */
  742.         close(outfd);
  743. #else /* !MTS */
  744.         set_file_time_and_close();
  745. #endif /* ?MTS */
  746. #endif /* ?VMS */
  747.  
  748. #ifdef S_IFLNK
  749.     } /* endif (!symlnk) */
  750. #endif /* S_IFLNK */
  751.  
  752.     if ((crc32val = ((~crc32val) & 0xFFFFFFFFL)) != lrec.crc32) {
  753.         /* if quietflg is set, we haven't output the filename yet:  do it */
  754.         if (quietflg)
  755.             printf("%-22s: ", filename);
  756.         fprintf(stdout, " Bad CRC %08lx  (should be %08lx)\n", crc32val,
  757.                 lrec.crc32);
  758.         error = 1;              /* 1:  warning error */
  759.     } else if (tflag) {
  760.         if (!quietflg)
  761.             fprintf(stdout, " OK\n");
  762.     } else {
  763.         if (QCOND && !error)
  764.             fprintf(stdout, "\n");
  765.     }
  766.  
  767.     return error;
  768.  
  769. }       /* end function extract_or_test_member() */
  770.  
  771.  
  772.  
  773.  
  774.  
  775. #ifdef CRYPT
  776.  
  777. /*******************************/
  778. /*  Function decrypt_member()  */
  779. /*******************************/
  780.  
  781. static int decrypt_member()   /* return 10 if out of memory or can't get */
  782. {                             /*  tty; -1 if bad password; 0 if checks out */
  783.     UWORD b, c;
  784.     byte *p;
  785.     int n, t;
  786.  
  787.     /* set keys */
  788.     if (key == NULL && ((key = malloc(PWLEN+1)) == NULL ||
  789.          getp("Enter password: ", key, PWLEN+1) == NULL))
  790.         return 10;        /* what to return here? */
  791.     init_keys(key);
  792.  
  793.     /* decrypt current buffer contents, set flag to re-read if necessary */
  794.     for (n = incnt > csize ? (int)csize : incnt, p = inptr; n--; p++)
  795.         *p = DECRYPT(*p);
  796.  
  797.     /* decrypt header */
  798.     for (c = 0; c < 10; c++)
  799.         ReadByte(&b);
  800.     ReadByte(&c);
  801.     ReadByte(&b);
  802. #ifdef CRYPT_DEBUG
  803.     printf("   lrec.crc = %08lx   crec.crc = [ ? ]   pInfo->ExtLocHdr = %s\n",
  804.       lrec.crc32, /* pInfo->crc, */ pInfo->ExtLocHdr? "true":"false");
  805.     printf("   incnt = %d   unzip offset into zipfile = %ld\n", incnt,
  806.       cur_zipfile_bufstart+(inptr-inbuf));
  807.     printf("   (c | (b<<8)) = %04x   (crc >> 16) = %04x   lrec.time = %04x\n",
  808.       (UWORD)(c | (b<<8)), (UWORD)(lrec.crc32 >> 16), lrec.last_mod_file_time);
  809. #endif /* CRYPT_DEBUG */
  810.     /* same test as in zipbare() in crypt.c */
  811.     if ((UWORD)(c | (b<<8)) != (pInfo->ExtLocHdr? lrec.last_mod_file_time :
  812.         (UWORD)(lrec.crc32 >> 16)))
  813.         return -1;
  814.     return 0;
  815. }
  816.  
  817. #endif /* CRYPT */
  818.